package com.ifacebox.client.tools;

import com.ifacebox.client.tools.utils.ImageUtils;
import org.apache.commons.io.FileUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * @author znn
 */
public class FaceLuckDrawSelectionDialog extends JDialog {
    private static final long serialVersionUID = 1L;
    private static final int FACE_IMAGE_MULTIPLE_SIZE = 60;
    private static final int FACE_PERSON_ONE_FONT = 32;
    private static final int FACE_PERSON_MULTIPLE_FONT = 24;
    private final FaceLuckDrawConfig faceLuckDrawConfig;
    private ImageIcon luckDrawTopImage, luckDrawCenterImage, luckDrawBottomImage;
    private JPanel luckDrawCenterPanel;
    private Box luckDrawCenterBox;
    private int x, y;

    public FaceLuckDrawSelectionDialog(Frame frame, FaceLuckDrawConfig faceLuckDrawConfig) {
        super(frame);
        this.faceLuckDrawConfig = faceLuckDrawConfig;
        super.setTitle(frame.getTitle());
        super.setIconImage(frame.getIconImage());
        super.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        super.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                x = e.getX();
                y = e.getY();
            }
        });
        super.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                setLocation(e.getXOnScreen() - x, e.getYOnScreen() - y);
            }
        });
        super.setUndecorated(true);
        super.setBackground(new Color(0, 0, 0, 0));
        this.initComponent();
        super.add(new JLabel(luckDrawTopImage), BorderLayout.NORTH);
        super.add(luckDrawCenterPanel, BorderLayout.CENTER);
        super.add(new JLabel(luckDrawBottomImage), BorderLayout.SOUTH);
        super.pack();
        super.setResizable(false);
        super.setLocationRelativeTo(null);
    }

    public void initComponent() {
        luckDrawTopImage = new ImageIcon(this.getClass().getResource("/static/face-luck-draw-top.png"));
        luckDrawCenterImage = new ImageIcon(this.getClass().getResource("/static/face-luck-draw-center.png"));
        luckDrawCenterPanel = new JPanel() {
            private static final long serialVersionUID = 1L;

            @Override
            protected void paintComponent(Graphics g) {
                g.drawImage(luckDrawCenterImage.getImage(), 0, 0, luckDrawCenterImage.getImage().getWidth(this), luckDrawCenterImage.getImage().getHeight(this), this);
            }
        };
        luckDrawBottomImage = new ImageIcon(this.getClass().getResource("/static/face-luck-draw-bottom.png"));
        luckDrawCenterPanel.add(luckDrawCenterBox = Box.createVerticalBox(), BorderLayout.CENTER);
    }

    public void faceLuckDrawSelection(Component comp, List<FaceImagePerson> faceImagePersons) {
        luckDrawCenterBox.removeAll();
        for (FaceImagePerson faceImagePerson : faceImagePersons) {
            FacePersonInfo facePersonInfo = faceImagePerson.getFacePersonInfo();
            if (facePersonInfo == null) {
                facePersonInfo = new FacePersonInfo();
            }
            ImageIcon faceImage = new ImageIcon(faceImagePerson.getFaceFile().toURI().getPath());
            JLabel facePerson = new JLabel("", JLabel.LEFT);
            facePerson.setForeground(Color.WHITE);
            facePerson.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            if (faceImagePersons.size() == 1) {
                facePerson.setVerticalTextPosition(JLabel.BOTTOM);
                facePerson.setHorizontalTextPosition(JLabel.CENTER);
                facePerson.setFont(new Font("", Font.BOLD, FACE_PERSON_ONE_FONT));
            } else {
                facePerson.setVerticalTextPosition(JLabel.CENTER);
                facePerson.setHorizontalTextPosition(JLabel.TRAILING);
                facePerson.setFont(new Font("", Font.BOLD, FACE_PERSON_MULTIPLE_FONT));
                faceImage = new ImageIcon(ImageUtils.resizeImage(faceImage.getImage(), FACE_IMAGE_MULTIPLE_SIZE, FACE_IMAGE_MULTIPLE_SIZE, Image.SCALE_SMOOTH));
            }
            facePerson.setIcon(faceImage);
            facePerson.setText(facePersonInfo.toTips(faceLuckDrawConfig.getFaceLuckDrawTips()));
            luckDrawCenterBox.add(facePerson);
            try {
                FileUtils.writeLines(faceLuckDrawConfig.getFaceLuckDrawList(), "UTF-8", Collections.singletonList(facePersonInfo.toString()), true);
            } catch (IOException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
        super.pack();
        super.setLocationRelativeTo(comp);
    }
}
